/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata.api;

import android.content.Context;

import trs.wmata.BusStop;
import static trs.wmata.api.ApiXml.A_STRING;
import static trs.wmata.api.ApiXml.LAT;
import static trs.wmata.api.ApiXml.LON;
import static trs.wmata.api.ApiXml.NAME;
import static trs.wmata.api.ApiXml.ROUTES;
import static trs.wmata.api.ApiXml.STOP;
import static trs.wmata.api.ApiXml.STOP_ID;

import java.util.ArrayList;

import org.xml.sax.Attributes;

public final class BusStops extends ApiResponse {
    private BusStop busStop = null;
    private ArrayList<String> routeCodes = null;
    private final ArrayList<BusStop> busStops = new ArrayList<BusStop>();
    private final Context context;

    public BusStops(final Context context) {
        this.context = context;
    }

    public ArrayList<BusStop> getBusStops() {
        return busStops;
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        // } else if (StopsResp.equals(lName) ) {
        // } else if (Stops.equals(lName) ) {
        if (STOP.equals(lName)) {
            busStop = new BusStop(context);

            // } else if (Lat.equals(lName) ) {
            // } else if (Lon.equals(lName) ) {
            // } else if (Name.equals(lName) ) {
        } else if (ROUTES.equals(lName)) {
            routeCodes = new ArrayList<String>(10);

            // } else if (a_string.equals(lName) ) {
            // } else if (StopID.equals(lName) ) {
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        if (STOP.equals(lName)) {
            if (busStop != null) {
                busStops.add(busStop);
                busStop = null;
            }
        } else if (busStop != null) {
            final String str = buff.toString().trim();

            if (LAT.equals(lName)) {
                busStop.setLat(toDouble(str, 0));
            } else if (LON.equals(lName)) {
                busStop.setLon(toDouble(str, 0));
            } else if (NAME.equals(lName)) {
                busStop.setName(str);
            } else if (STOP_ID.equals(lName)) {
                busStop.setCode(str);
            } else if (routeCodes != null) {
                if (A_STRING.equals(lName)) {
                    routeCodes.add(buff.toString().trim());
                } else if (ROUTES.equals(lName)) {
                    busStop.setRouteCodes(routeCodes.toArray(new String[routeCodes.size()]));
                    routeCodes = null;
                }
            }
        }
    }

    @Override
    public boolean doCaptureChars() {
        return (busStop != null) || (routeCodes != null);
    }
}
